﻿using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[assembly: TagPrefix("AspDotnetPractice.CustomServerControl", "practiceUI")]
namespace AspDotnetPractice.CustomServerControl
{
    [AspNetHostingPermission(SecurityAction.Demand,Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ParseChildren(true,"Text")]
    [DefaultProperty("Email")]
    [ToolboxData("<{0}:MailLink ID='MailLinkID' Text='WebMaster' runat=\"server\"> </{0}:MailLink>")]
    public class MailLink : WebControl
    {
        [Category("Appearance")]
        [Browsable(true)]
        [DefaultValue("yuxinggao@gmail.com")]
        [Description("The e-mail address.")]
        public virtual string Email
        {
            get
            {
                string email = (string)ViewState["Email"];
                return email ?? "yuxinggao@gmail.com";
            }
            set { ViewState["Email"] = value; }
        }

        [Category("Appearance")]
        [Browsable(true)]
        [DefaultValue("Web Master")]
        [Description("The name to display.")]
        [Localizable(true)]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public virtual string Text
        {
            get
            {
                string text = (string)ViewState["Text"];
                return text ?? string.Empty;
            }
            set { ViewState["Text"] = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.A; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Href,string.Concat("mailto:",Email));
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);

            if (Text == string.Empty) Text = Email;
            writer.WriteEncodedText(Text);
        }

    }
}